package com.lablzh.gdxy.component;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class POIUtil {
    public static boolean writeIntoExcel(String filePath, String tableTitle
            , ArrayList<String> tableHeader,
                                         List<ArrayList<String>> rowData) {
        File file = new File(filePath);
        // 判断目标文件所在的目录是否存在，不在则创建
        if (!file.getParentFile().exists()) {
            if (!file.getParentFile().mkdirs()) {
                System.out.println("文件所在目录不存在，创建失败");
                return false;
            }
        }
        // 创建工作簿
        HSSFWorkbook wb;
        // 创建表对象
        HSSFSheet sheet;
        // 创建行
        HSSFRow row;
        // 输入输出流
        FileInputStream fis = null;
        OutputStream out = null;
        try {
            if (file.isFile()) {
                // 存在，则直接追加数据
                fis = new FileInputStream(file);
                POIFSFileSystem ps = new POIFSFileSystem(fis);
                // 1.获取excel的book对象
                wb = new HSSFWorkbook(ps);
                // 2.获取sheet
                sheet = wb.getSheetAt(0);
                // 3.获取插入数据的行
                row = sheet.createRow((short) (sheet.getLastRowNum() + 1));
            } else {
                // 不存在，则新建excel
                // 1.获取excel的book对象
                wb = new HSSFWorkbook();
                // 2.创建sheet
                sheet = wb.createSheet(tableTitle);
                // 居中样式
                HSSFCellStyle style = wb.createCellStyle();
//                style.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
//                style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
                // 字体样式
                HSSFFont font = wb.createFont();
//                font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//                font.setItalic(true);
                font.setFontHeight((short) 200);
                font.setColor(HSSFFont.COLOR_NORMAL);
                style.setFont(font);
                // 3.创建第一行，作为标题
                HSSFRow row0 = sheet.createRow(0);
                sheet.addMergedRegion(new CellRangeAddress(0, (short) 0, 0, (short) tableHeader.size()));
                HSSFCell row0cell0 = row0.createCell(0);
                row0cell0.setCellValue(tableTitle);
                row0cell0.setCellStyle(style);
                // 4.创建第二行，作为表头
                HSSFRow row1 = sheet.createRow(1);
                for (int i = 0; i < tableHeader.size(); ++i) {
                    HSSFCell row1cell = row1.createCell(i);
                    row1cell.setCellValue(tableHeader.get(i));
                    row1cell.setCellStyle(style);
                }
                for(int i=2;i<rowData.size()+2;i++){
                    row = sheet.createRow(i);
                    for(int j = 0; j < tableHeader.size(); ++j){
                        row.createCell(j).setCellValue(rowData.get(i-2).get(j));
                    }
                }
            }

            // 写文件
            out = new FileOutputStream(file);
            out.flush();
            wb.write(out);
            return true;
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}
